package com.huilian.iotbox.admin.service.impl;

import com.huilian.iotbox.admin.dao.StatsPackageUserDao;
import com.huilian.iotbox.admin.service.StatsPackageUserService;
import com.huilian.iotbox.data.enums.StatsPackageStorePackageTypeEnum;
import com.huilian.iotbox.data.po.StatsPackageUser;
import com.huilian.iotbox.data.service.ErrorLogCommonService;
import com.huilian.iotbox.data.vo.StatsPackageUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LinYinDong
 * @date 2023/7/28 15:14
 */
@Service
public class StatsDayPackageUserServiceImpl implements StatsPackageUserService {
    @Resource
    private StatsPackageUserDao statsPackageUserDao;
    @Autowired
    private ErrorLogCommonService errorLogCommonService;

    @Override
    public Exception statsDayPackageUserJob(String startTime, String endTime, String yesterday, Integer type) {
        Exception temp = null;
        try {
            //根据场地统计门店套餐
            List<StatsPackageUserVo> statsPackageUserVoList = statsPackageUserDao.statsOrderUserIdStoreList(startTime, endTime);
            if(!ObjectUtils.isEmpty(statsPackageUserVoList)){
                statsPackageUserVoList.forEach(item ->{
                    StatsPackageUser statsPackageUser = new StatsPackageUser();
                    statsPackageUser.setUserId(item.getUserId());
                    statsPackageUser.setType(type);
                    statsPackageUser.setDateStr(yesterday);
                    statsPackageUser.setPackageType(StatsPackageStorePackageTypeEnum.STORE_PACKAGE.getCode());
                    statsPackageUser.setTitle(item.getTitle());
                    statsPackageUser.setTotalAmount(item.getTotalAmount());
                    statsPackageUser.setPayCount(item.getPayCount());
                    statsPackageUserDao.insert(statsPackageUser);
                });
            }

            //根据场地统计抖音套餐
            List<StatsPackageUserVo> statsPackageDouYinList = statsPackageUserDao.statsPackageUserDouYinList(startTime, endTime);
            if(!ObjectUtils.isEmpty(statsPackageDouYinList)){
                statsPackageDouYinList.forEach(item ->{
                    StatsPackageUser statsPackageUser = new StatsPackageUser();
                    statsPackageUser.setUserId(item.getUserId());
                    statsPackageUser.setType(type);
                    statsPackageUser.setDateStr(yesterday);
                    statsPackageUser.setPackageType(StatsPackageStorePackageTypeEnum.DOUYIN_PACKAGE.getCode());
                    statsPackageUser.setTitle(item.getTitle());
                    statsPackageUser.setTotalAmount(item.getTotalAmount());
                    statsPackageUser.setPayCount(item.getPayCount());
                    statsPackageUserDao.insert(statsPackageUser);
                });
            }

            //根据场地统计美团套餐
            List<StatsPackageUserVo> statsPackageMeiTuanList = statsPackageUserDao.statsPackageUserMeiTuanList(startTime, endTime);
            if(!ObjectUtils.isEmpty(statsPackageMeiTuanList)){
                statsPackageMeiTuanList.forEach(item ->{
                    StatsPackageUser statsPackageUser = new StatsPackageUser();
                    statsPackageUser.setUserId(item.getUserId());
                    statsPackageUser.setType(type);
                    statsPackageUser.setDateStr(yesterday);
                    statsPackageUser.setPackageType(StatsPackageStorePackageTypeEnum.MEITUAN_PACKAGE.getCode());
                    statsPackageUser.setTitle(item.getTitle());
                    statsPackageUser.setTotalAmount(item.getTotalAmount());
                    statsPackageUser.setPayCount(item.getPayCount());
                    statsPackageUserDao.insert(statsPackageUser);
                });
            }
        }catch (Exception e) {
            e.printStackTrace();
            temp = e;
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

            StatsPackageUserVo statsPackageUserVo = new StatsPackageUserVo();
            statsPackageUserVo.setStartTime(startTime);
            statsPackageUserVo.setEndTime(endTime);
            errorLogCommonService.writerInfoException("套餐订单日统计失败", statsPackageUserVo, e);
        }
        return temp;
    }

    @Override
    public Exception statsMonthPackageUserJob(String startTime, String endTime, String previousMonth, Integer type) {
        return this.statsDayPackageUserJob(startTime,endTime,previousMonth,type);
    }
}
